package com.softeem.god.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.god.comm.R;
import com.softeem.god.entity.Admin;
import com.softeem.god.mapper.AdminMapper;
import com.softeem.god.service.AdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
@Tag(name = "管理员管理")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;
    
    @Operation(summary = "管理员列表",description = "分页查询管理员列表")
    @Parameters(value = {@Parameter(name = "page",description = "当前页码"),
            @Parameter(name = "limit",description = "每页显示数量")})
    @GetMapping("/admin/list")
    public R list(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5") Integer limit) {
        IPage<Admin> ipage = new Page<>(page,limit);
        IPage<Admin> result = adminService.page(ipage);
        long count = adminService.count();

        return R.success().setData(result).setCount( count);
    }
    
    @Operation(summary = "添加管理员",description = "添加管理员")
    @PostMapping("/admin/add")
    public R addAdmin(@RequestBody Admin admin) {
        // 设置创建时间
        admin.setCreateTime(new Timestamp(new Date().getTime()));
        // 默认状态为启用
        admin.setStatus(1);
        return adminService.add(admin);
    }
    
    @Operation(summary = "修改管理员",description = "根据id修改管理员")
    //http请求方法分类：增加:post  删除:delete  修改:put  查询:get
    @PutMapping("/admin/update")
    public R update(@RequestBody Admin admin) {
        boolean success = adminService.updateById(admin);
        if(success) {
            return R.success();
        } else {
            return R.error().setMsg("修改失败");
        }
    }
    
    @Operation(summary = "删除管理员",description = "根据id删除管理员")
    @Parameters(value =
            {@Parameter(name = "id",description = "管理员id")
            })
    @DeleteMapping("/admin/delete")
    public R delete(Integer id) {
        boolean success = adminService.removeById(id);
        if(success) {
            return R.success();
        } else {
            return R.error().setMsg("删除失败");
        }
    }
    
    @Operation(summary = "管理员登录",description = "根据用户名和密码登录")
    @Parameters({@Parameter(name = "username",description = "用户名"),
            @Parameter(name = "password",description = "密码")
    })
    @GetMapping("admin/login")
    public R login(String username, String password) {
        return adminService.login(username, password);
     }
}




